package com.scsystem.xysc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scsystem.xysc.mapper.UserMapper;
import com.scsystem.xysc.pojo.User;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.UserService;
import com.scsystem.xysc.util.Images;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author zhoub
 */
@RestController
@Transactional
@CrossOrigin
@Tag(name = "图片接口")
@RequestMapping("/Image")
public class ImageController {

    @Resource
    UserMapper userMapper;
    @Resource
    UserService userService;
    @Value("${file.upload-path}")
    private String path;

    /**
     * 上传图片
     *
     * @param file 图片文件
     * @return 图片的url
     */
    @PostMapping("userImage")
    @Operation(summary = "上传图片")
    public ReturnResult<Boolean> Image(HttpServletRequest request,@RequestPart(value = "file", required = false)
                                       @Parameter(description = "图片上传", required = true, content = @Content(mediaType = "multipart/form-data")
                                       ) MultipartFile file) {
        HttpSession session = request.getSession();
        Long userid = (Long) session.getAttribute("userid");
        if (userid == null) {
            return new ReturnResult<>(500, "请先登录", false);
        }
        userMapper.selectById(userid);
        String s = Images.MultipartFile(file, "http://www.sctianji.com:10001/images/xysc/", path);
        userService.updateImage(s, userid);
        return new ReturnResult<>(200, "上传成功", true);
    }
}